package com.safx.utility;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

/**
 * 
 * <b>一些常用工具集合 </b>
 * 
 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
 */
public class CommonTool {
	static Logger logger = Logger.getLogger(CommonTool.class);

	/**
	 * 
	 * <b>获得文件后缀名</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public static String getExtensionName(File file) {
		String[] strings = file.getName().split("\\.");
		int length = strings.length;
		return strings[length - 1];
	}

	/**
	 * 将String，Boolean，Number类型转化为字符创 不能转化的返回""。
	 * 
	 * @param o
	 * @return
	 */
	public static String any2String(Object o) {
		if (o instanceof String) {
			return (String) o;
		} else if (o instanceof Boolean) {
			return "" + (Boolean) o;
		} else if (o instanceof Number) {
			return "" + (Number) o;
		} else {
			logger.error("if you see this, please check method 'CommonTool.any2String()'. " + o);
			return "";
		}
	}

	/**
	 * 
	 * <b> </b> <br/>
	 * 字符串转化为整数，默认值是0
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-9
	 */
	public static int String2Int(String str) {
		int intTime = 0;
		try {
			intTime = Integer.valueOf(str);
		} catch (NumberFormatException e) {
			logger.warn(e.getMessage() + ", use default one: 0.");
		}
		return intTime;
	}

	/**
	 * 
	 * <b>读取文件集合，xml后缀的</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public static List<File> readXmlFileList(String path) {
		return readFileListAccordingToExtensionName(path, "xml", new ArrayList<File>());
	}

	/**
	 * 
	 * <b>读取文件集合，xml后缀的</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public static List<File> readFileListAccordingToExtensionName(String path, String extensionName, List<File> caseList) {

		File file = new File(path);

		/**
		 * if the path is a file
		 */
		if (file.isFile()) {
			caseList.add(file);
			return caseList;
		}

		File[] files = new File(path).listFiles();

		if (files == null)
			throw new RuntimeException("The path " + path + " in config is wrong.");

		for (File testCase : files) {
			if (testCase.isDirectory()) {
				readFileListAccordingToExtensionName(testCase.getPath(), extensionName, caseList);
			}
			if (CommonTool.getExtensionName(testCase).equals(extensionName)) {
				caseList.add(testCase);
			}
		}
		return caseList;
	}

	/**
	 * 
	 * <b> </b> 空或者null返回true,其他false;
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public static boolean isEmpty(String str) {
		if (str == null || str.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * <b></b><br>
	 * 只有把<code>#fff</code>之类的颜色转化为<code>#ffffff</code>六位的，还有<code>white</code>
	 * 转为<code>#ffffff</code>。 其他的值直接返回。
	 * 
	 * @param color
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-2-17
	 */
	public static String threeToSixBit(String color) {
		String expressionThreeBit = "^#[0-f]{3}$";
		String redBuffer, greenBuffer, blueBuffer;
		String rgbResult = color;
		if (matcherColor(expressionThreeBit, color)) {
			redBuffer = String.valueOf(color.charAt(1));
			redBuffer += redBuffer;
			greenBuffer = String.valueOf(color.charAt(2));
			greenBuffer += greenBuffer;
			blueBuffer = String.valueOf(color.charAt(3));
			blueBuffer += blueBuffer;
			rgbResult = "#" + redBuffer + greenBuffer + blueBuffer;
		} else if (color.equals("white")) {
			rgbResult = "#ffffff";
		} else if (color.equals("black")) {
			rgbResult = "#000000";
		} else if (color.equals("green")) {
			rgbResult = "#008000";
		}
		return rgbResult;
	}

	/**
	 * 只有把<code>#fff</code>之类的颜色转化为<code>#ffffff</code>六位的，还有<code>white</code>
	 * 转为<code>#ffffff</code>。 其他的值直接返回。
	 * 
	 * @param expression
	 * @param color
	 * @return
	 */
	public static boolean matcherColor(String expression, String color) {
		Pattern p = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(color);
		return m.matches();
	}
	
	
}
